// "Java Tech"
//  Code provided with book for educational purposes only.
//  No warranty or guarantee implied.
//  This code freely available. No copyright claimed.
//  2003
//
package dcartes.plot;

import java.awt.Container;

import javax.swing.JApplet;


// Begin from StartJApplet1.java

/*
 <applet code="PlotDrawApplet.class" width=200 height=200>
  </applet>
*/

/**
 *  Demonstrate the DrawPanel, which invokes the draw methods
 *  of a list of DrawFunction objects. This illustrates how to
 *  separate the display surface from the drawings in a modular
 *  approach.
 */
public class PlotDrawApplet extends JApplet
{
  public void init () {
    Container content_pane = getContentPane ();

    // Create a line and some points along the line
    double y_data_min = 0.0;
    double y_data_max = 100.0;
    double x_data_min = 0.0;
    double x_data_max = 500.0;

    double y_intercept = 10.0;
    double slope      = 0.16;

    double [] line_parameters = new double[2];
    line_parameters[0] = y_intercept;
    line_parameters[1] = slope;

    // Create the object to draw a straight line.
    DrawLine draw_line = new DrawLine ();

    draw_line.setParameters (line_parameters, null);

    // Create the points data
    double [] y = new double[10];
    double [] x = new double[10];
    double [] y_err = new double[10];

    // Need a 2 D array to pass to DrawPoints object.
    double [][] data = new double[4][];
    data[0] = y;
    data[1] = x;
    data[2] = y_err;
    data[3] = null;// no error on x coordinate

    // Create some data points using the line.
    double x_pos = 45.0;
    double err  =  4.0;

    for (int i=0; i < 10; i++){
        y[i] = y_intercept + slope * x_pos;
        x[i] = x_pos;
        y_err[i] = err;
        err *= 1.20; // let error increase with x
        x_pos += 45;
    }

    // Create a DrawPoints instance to draw the points on
    // the DrawPanel.
    DrawPoints draw_points = new DrawPoints ();
    draw_points.setParameters (null, data);

    DrawFunction  [] drawFunctions = new DrawFunction[2];

    drawFunctions[0] = draw_line;
    drawFunctions[1] = draw_points;

    // Create an instance of DrawPanel with the range limits and
    // the functions to draw.
    DrawPanel draw_panel = new DrawPanel (y_data_min, y_data_max,
                                          x_data_min,  x_data_max,
                                          drawFunctions);

    // And add one or more panels to the JApplet panel.
    content_pane.add (draw_panel);

  } // init

} // class PlotDrawApplet

